package com.pulaien.web.controller.fundmanagement;


import com.pulaien.common.annotation.Log;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.enums.BusinessType;
import com.pulaien.common.utils.ConvertUtils;
import com.pulaien.common.utils.StringUtils;
import com.pulaien.common.utils.poi.ExcelUtil;
import com.pulaien.system.domain.CustomerBill;
import com.pulaien.system.domain.CustomerBill2;
import com.pulaien.system.domain.dto.CustomerBillEN;
import com.pulaien.system.domain.dto.CustomerBillJA;
import com.pulaien.system.domain.dto.fundmanagement.ExtCustomerBillModel;
import com.pulaien.system.service.fundmanagement.CmUserCapitalRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户端资金明细Controller
 * Created by GHN on 2021/03/26.
 */
@Controller
@Api(tags = "买家端-资金明细", description = "买家端-资金明细")
@RequestMapping("/shop/cmUser/capitalRecord")
public class CmUserCapitalRecordController extends BaseController {

    private final CmUserCapitalRecordService cmUserCapitalRecordService;

    public CmUserCapitalRecordController(CmUserCapitalRecordService cmUserCapitalRecordService) {
        this.cmUserCapitalRecordService = cmUserCapitalRecordService;
    }

    @ApiOperation(value = "用户资金列表分页展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public TableDataInfo list(
            ExtCustomerBillModel recharge
    ) {
        startPage();
        List<CustomerBill> record = cmUserCapitalRecordService.list(recharge);
        return getDataTable(record);
    }


    @ApiOperation(value = "导出资金记录列表")
    @Log(title = "导出资金记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExtCustomerBillModel recharge, HttpServletRequest request)
    {

        //recharge.setIsCustomerOrder("1");
        List<CustomerBill2> record = cmUserCapitalRecordService.list2(recharge);

        List<CustomerBill2> recordZHs = new ArrayList<>();

        List<CustomerBillJA> customerBillJAs = new ArrayList<>();

        List<CustomerBillEN> customerBillENs = new ArrayList<>();

        for (CustomerBill2 s : record){
            CustomerBillEN customerBillEN = ConvertUtils.sourceToTarget(s, CustomerBillEN.class);
            CustomerBillJA customerBillJA = ConvertUtils.sourceToTarget(s, CustomerBillJA.class);

            String typeDetail = s.getTypeDetail();
            if("1".equals(typeDetail)) {
                s.setTypeDetail("充值");
                customerBillEN.setTypeDetail("top-up");
                customerBillJA.setTypeDetail("チャージ");
            }
            if("2".equals(typeDetail)) {
                s.setTypeDetail("装箱单付款");
                customerBillEN.setTypeDetail("梱包ロット費用支払い");
                customerBillJA.setTypeDetail("Packing list payment");
            }
            if("3".equals(typeDetail)) {
                s.setTypeDetail("订单付款");
                customerBillEN.setTypeDetail("Payment on order");
                customerBillJA.setTypeDetail("注文支払い");
            }
            if("4".equals(typeDetail)) {
                s.setTypeDetail("赔偿");
                customerBillEN.setTypeDetail("補償");
                customerBillJA.setTypeDetail("Compensate for");
            }
            if("5".equals(typeDetail)) {
                s.setTypeDetail("扣款");
                customerBillEN.setTypeDetail("費用控除");
                customerBillJA.setTypeDetail("Deduct money");
            }
            if("6".equals(typeDetail)) {
                s.setTypeDetail("退金（提现）");
                customerBillEN.setTypeDetail("返金（引き出し）");
                customerBillJA.setTypeDetail("Refund (withdrawal)");
            }
            if("7".equals(typeDetail)) {
                s.setTypeDetail("购买完扣费");
                customerBillEN.setTypeDetail("購入商品費用控除");
                customerBillJA.setTypeDetail("Deductible fee after purchase");
            }
            if("8".equals(typeDetail)) {
                s.setTypeDetail("退货退费");
                customerBillEN.setTypeDetail("返品返金");
                customerBillJA.setTypeDetail("Return fee");
            }
            if("9".equals(typeDetail)) {
                s.setTypeDetail("异常扣费");
                customerBillEN.setTypeDetail("異常控除");
                customerBillJA.setTypeDetail("Abnormal deduction");
            }
            if("10".equals(typeDetail)) {
                s.setTypeDetail("同捆付款");
                customerBillEN.setTypeDetail("同梱支払い");
                customerBillJA.setTypeDetail("Lump-sum payment");
            }
            if("11".equals(typeDetail)) {
                s.setTypeDetail("仓库保管费");
                customerBillEN.setTypeDetail("倉庫保管料");
                customerBillJA.setTypeDetail("Warehouse storage fees");
            }
            if("12".equals(typeDetail)) {
                s.setTypeDetail("检品完扣款");
                customerBillEN.setTypeDetail("検品完了後差し引く");
                customerBillJA.setTypeDetail("Deduction after inspection");
            }

            String paymentMethod = s.getPaymentMethod();
            if("0".equals(paymentMethod)) {
                s.setPaymentMethod("余额");
                customerBillEN.setTypeDetail("残高");
                customerBillJA.setTypeDetail("balance");
            }
            if("1".equals(paymentMethod)) {
                s.setPaymentMethod("paypal");
                customerBillEN.setTypeDetail("paypal");
                customerBillJA.setTypeDetail("paypal");
            }

            String moneyFlag = s.getMoneyFlag();
            if("0".equals(moneyFlag)) {
                s.setMoneyFlag("出");
                customerBillEN.setTypeDetail("Out of the");
                customerBillJA.setTypeDetail("出ます");
            }

            if("1".equals(moneyFlag)) {
                s.setMoneyFlag("入");
                customerBillEN.setTypeDetail("Into the");
                customerBillJA.setTypeDetail("入ります");
            }

            recordZHs.add(s);
            customerBillJAs.add(customerBillJA);
            customerBillENs.add(customerBillEN);
        }

        //根据不同语言对应不同模板
        String acceptLanguage = request.getHeader("Accept-Language");
        if (StringUtils.isNotBlank(acceptLanguage) && "ja-ja".equals(acceptLanguage)){
            ExcelUtil<CustomerBillJA> util = new ExcelUtil<>(CustomerBillJA.class);
            util.exportExcel(response, customerBillJAs, "資金記録です");
        }else if (StringUtils.isNotBlank(acceptLanguage) && "en-us".equals(acceptLanguage)){
            ExcelUtil<CustomerBillEN> util = new ExcelUtil<>(CustomerBillEN.class);
            util.exportExcel(response, customerBillENs, "Fund record");
        }else {
            ExcelUtil<CustomerBill2> util = new ExcelUtil<>(CustomerBill2.class);
            util.exportExcel(response, recordZHs, "资金记录");
        }
    }

}
